<template>
	<uni-popup ref="popup" type="bottom" @change="change">
		<view class="popup-content d-flex flex-column">
			<view class="d-flex justify-content-end">
				<image src="/static/images/common/clousex-big.png" class="close_btn" @tap="close"></image>
			</view>
			<view class="d-flex flex-fill flex-column justify-content-between">
				<view class="d-flex flex-column">
					<view class="font-size-extra-lg font-weight-bold text-color-base mb-30">欢迎登陆喜茶星球~</view>
					<view class="font-size-base text-color-assist">登录后消费可获取积分，享受更好的服务体验</view>
				</view>
				<view class="d-flex flex-column">
					<button type="primary" class="w-100 font-size-lg mb-30" open-type="getUserInfo" @getuserinfo="getUserInfo">微信一键登陆</button>
					<view class="text-center mb-30 font-size-sm text-color-assist">
						点击登陆喜茶GO，即表示已阅读并同意<font class="text-color-primary">《喜茶隐私政策》</font>
					</view>
					<view class="text-center font-primary font-size-sm text-color-primary">《喜茶GO小程序服务指南》</view>
				</view>
			</view>
		</view>
	</uni-popup>
</template>

<script>
	import uniPopup from '@/components/uni-popup/uni-popup.vue'
	import { mapMutations } from 'vuex'
	import request from '../../../common/request'
	
	export default {
		name: 'LoginPopup',
		components: {
			uniPopup
		},
		props: {
			
		},
		data(){
			return{
				Code:''//微信临时登录凭证
			}
		},
		methods: {
			...mapMutations(['SET_USERINFO', 'SET_ISLOGIN']),
			open() {
				this.$refs['popup'].open()
			},
			close() {
				this.$refs['popup'].close()
			},
			change({show}) {
				this.$emit('change', show)
			},
			
			getUserInfo(e) {

				
				let openId='';
				// #ifdef MP-WEIXIN
				uni.getProvider({
					service: 'oauth',
					success: function (res) {
					if (~res.provider.indexOf('weixin')) {
						uni.login({
							provider: 'weixin',
							success: (res2) => {
								if (res2.errMsg == "login:ok") {
									console.log("code:"+res2.code);
							
									
									let appid = 'wx50a66cce7305ae30'
									let secret = '326aa53403e004e20592b91095a4f800'
									let url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appid + '&secret=' + secret + '&js_code=' +res2.code + '&grant_type=authorization_code&state=STATE';
									console.log("url:"+url);
									uni.request({
										url: url, // 请求路径
										success: result => {
												console.info("openid:"+result.data.openid);
												openId=result.data.openid;
												
												//存储到storage,在别的页面也能获取到openid
												uni.setStorageSync("openid",result.data.openid);
												
												// console.log("openId:"+openId);
												/* uni.request({
													url: "http://localhost:8001/eduservice/openid/save/"+result.data.openid,
													method: "POST",
													data: {
														content: submitdata
														
													},
													success:(res)=>{
														console.log(JSON.stringify(res));
														// console.log("res:"+res.data.data);
													}
												}); */
		
										},
										fail: () => {
											uni.showToast({title:"获取openId失败",icon:"none"});
										}
										});
										
									
								} 
								uni.getUserInfo({
									provider: 'weixin',
									success: (info) => {//这里请求接口
										console.log(res2);
										console.log(info);
										//请配置AppID，否则获取失败
										if(info.errMsg !== 'getUserInfo:ok') {
											uni.showModal({
												title: '提示',
												content: '您已取消了授权，请重新授权登录',
												showCancel: false
											})
											return
										}
										e.detail.userInfo=info.userInfo;
										
										
									},
									fail: () => {
										uni.showToast({title:"微信登录授权失败",icon:"none"});
									}
								})
						
							},
							fail: () => {
								uni.showToast({title:"微信登录授权失败",icon:"none"});
							}
						})
						
					}else{
						uni.showToast({
							title: '请先安装微信或升级版本',
							icon:"none"
						});
					}
					}
				});
				//#endif

				uni.getSetting({
				success(res) {
					console.log("授权：",res);
				if (!res.authSetting['e.detail.userInfo']) {
						//这里调用授权
						console.log("当前未授权");
				} else {
						//用户已经授权过了
						console.log("当前已授权");
				}
				}
			});
		
			
			
			if(e.target.errMsg !== 'getUserInfo:ok') {
				uni.showModal({
					title: '提示',
					content: '您已取消了授权，请重新授权登录',
					showCancel: false
				})
				return
			}
			
			// e.target.userInfo.add("openid",openId);
			
			
							
			this.SET_USERINFO(e.target.userInfo)
			this.SET_ISLOGIN(true)
			this.close()
		
			}
		}
	}
</script>

<style lang="scss" scoped>
	.popup-content {
		background-color: #FFFFFF;
		border-radius: 24rpx 24rpx 0 0;
		height: 50vh;
		padding: 50rpx 40rpx;
	}
	
	.close_btn {
		width: 40rpx;
		height: 40rpx;
	}
</style>
